package com.jiruan.invoicemanagementsystem.util;

import com.jiruan.invoicemanagementsystem.pojo.User;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

@SuppressWarnings("all")
//数据库处理工具类
public class DBHelper {
    @SuppressWarnings("all")
    //建立连接
    public static Connection getConnection() {
        Connection con = null;
        try{
            //1.手动添加数据库驱动类：
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获得数据库连接对象：
            String url = "jdbc:mysql://127.0.0.1:3306/socket?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
            String username = "root";
            String password = "123456";
            con = DriverManager.getConnection(url, username, password);
        }catch(Exception e){
            e.printStackTrace();
        }
        return con;
    }

    //通用 增 - 删 - 改
    @SuppressWarnings("all")
    public static int currencySqlUpdate(String sql,Object... objs){
        Connection con = getConnection();
        PreparedStatement ps = null;
        try{
            ps = con.prepareStatement(sql);
            for(int i = 1; i <= objs.length; i++){
                ps.setObject(i,objs[i-1]);
            }
            return ps.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            closeAll(con,ps,null);
        }
        return 0;
    }

    //通用返回集合的查询
    public static ArrayList currencySqlQuery(Class cla, String sql, Object... objs){
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        ArrayList<Object> backList = new ArrayList<>();
        try{
            con = getConnection();
            ps = con.prepareStatement(sql);
            for(int i = 1; i <= objs.length; i++){
                ps.setObject(i,objs[i-1]);
            }
            rs = ps.executeQuery();
            Field[] fields = cla.getDeclaredFields();
            while(rs.next()){
                Object o = cla.newInstance();  // 需要保证改该类有无参构造方法
                for(Field field : fields){
                    field.setAccessible(true);
                    field.set(o,rs.getObject(field.getName()));
                }
                backList.add(o);
            }
            return backList;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            closeAll(con,ps,rs);
        }
        return null;
    }


    //通用返回数量的查询
    public static Integer currencySqlQuery(String sql, Object... objs){
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            con =  getConnection();
            ps = con.prepareStatement(sql);
            for(int i = 1; i <= objs.length; i++){
                ps.setObject(i,objs[i-1]);
            }
            rs = ps.executeQuery();
            return  rs.next() ? rs.getInt(1) : 0;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(con,ps,rs);
        }
        return 0;
    }


    //资源关闭
    @SuppressWarnings("all")
    public static void closeAll(Connection con,PreparedStatement ps,ResultSet rs){
        try{
            if(rs!=null){
                rs.close();
            }
            if(ps!=null){
                ps.close();
            }
            if(con!=null){
                con.close();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
